package tuioZones;

import java.util.ArrayList;

public class DropArea {
	private float width,height;
	private PointXY coord;
	private ArrayList<Puzzle> finishedPuzzles;
	private ArrayList<TouchPoint> touchPoints;
	private int r,g,b;
    //used for setting the insede color of the zone
    private int r2,g2,b2;
    // only 9, if change rewrite the getPuzzleSize function
    private final int maxPuzzles = 9;
    private int angle;
    private PointXY translationPoint;
	
	public DropArea(float x,float y, float width, float height, int r, int g, int b, int angle) {
		this.coord = new PointXY(x,y);
		this.height = height;
		this.width = width;
		this.r = r;
		this.g = g;
		this.b = b;
        this.r2 = r;
        this.g2 = g;
        this.b2 = b;
		this.touchPoints = new ArrayList<TouchPoint>();
        this.finishedPuzzles = new ArrayList<Puzzle>();
        this.angle = angle;
	}

	/**
	 * @return the x
	 */
	public float getX() {
		return coord.getX();
	}

	/**
	 * @param x the x to set
	 */
	public void setX(float x) {
		this.coord.setX(x);
	}

	/**
	 * @return the y
	 */
	public float getY() {
		return coord.getY();
	}

	/**
	 * @param y the y to set
	 */
	public void setY(float y) {
		this.coord.setY(y);
	}

	/**
	 * @return the width
	 */
	public float getWidth() {
		return width;
	}

	/**
	 * @param width the width to set
	 */
	public void setWidth(float width) {
		this.width = width;
	}

	/**
	 * @return the height
	 */
	public float getHeight() {
		return height;
	}

	/**
	 * @param height the height to set
	 */
	public void setHeight(float height) {
		this.height = height;
	}

	/**
	 * @return the r
	 */
	public int getR() {
		return highlight() ? r + 20 : r;
	}

	/**
	 * @param r the r to set
	 */
	public void setR(int r) {
		this.r = r;
	}

	/**
	 * @return the g
	 */
	public int getG() {
		return highlight() ? g + 20 : g;
	}

	/**
	 * @param g the g to set
	 */
	public void setG(int g) {
		this.g = g;
	}

	/**
	 * @return the b
	 */
	public int getB() {
		return highlight() ? b + 20 : b;
	}

	/**
	 * @param b the b to set
	 */
	public void setB(int b) {
		this.b = b;
	}

	public boolean contains(TouchPoint tp) {
		return getX() <= tp.getX() && 
		       (getX()+getWidth()) >= tp.getX() && 
		       getY() <= tp.getY() && 
		       (getY() + getHeight()) >= tp.getY();
	}
	
	public void addTouchPoint(TouchPoint tp) {
		if(!touchPoints.contains(tp))
			touchPoints.add(tp);
	}
	
	public void removeTouchPoint(TouchPoint tp) {
		touchPoints.remove(tp);
	}

	public ArrayList<TouchPoint> getTouchPoints() {
		return touchPoints;
	}
	
	private boolean highlight() {
		return touchPoints.size() > 0;
	}

    public ArrayList<Puzzle> getFinishedPuzzles() {
        return finishedPuzzles;
    }

    public void removeAllPuzzles() {
        this.finishedPuzzles = new ArrayList<Puzzle>();
    }

    public void addPuzzle(Puzzle puzzle) {
        this.finishedPuzzles.add(puzzle);
    }

    /**
     * returns the size of a puzzle, displayed in the DropArea, according to the
     * width and height of the dropArea an the maximum number of Puzzles allowed.
     * @return size[0] = width, size[1] = height
     */
    private float[] getPuzzleSize() {
        float[] size = new float[2];
        float maxHeight = 0;
        float maxWidth = 0;
        if ((this.angle==90)||(this.angle==270)) {
            maxHeight = ((this.width-10)/3);
            maxWidth = (this.height/6);
        } else {
            maxHeight = ((this.height-10)/3);
            maxWidth = (this.width/6);
        }

        if (maxHeight>maxWidth) {
            size[0] = maxWidth*2;
            size[1] = maxWidth;
        } else if (maxHeight<maxWidth) {
            size[0] = maxHeight*2;
            size[1] = maxHeight;
        }
       
        return size;
    }

    public float getPuzzleWidth() {
        return getPuzzleSize()[0];
    }

    public float getPuzzleHeight() {
        return getPuzzleSize()[1];
    }

    public int getAngle() {
        return angle;
    }

    public void setAngle(int angle) {
        this.angle = angle;
    }

    public int getMaxPuzzles() {
        return maxPuzzles;
    }

    /**
     * returns the translation point for the dropArea, depending on its angle
     * @return
     */
    public PointXY getTranslationPoint() {        
        if (this.angle==0) { // blue
            this.translationPoint = new PointXY(this.getX() + 10, this.getY() + 7);
        } else if (this.angle==90) { // red
            this.translationPoint = new PointXY(this.getX() + this.getWidth() - 7, this.getY() + 10);
        } else if (this.angle==180) { // green
            this.translationPoint = new PointXY(this.getX() + this.getWidth() - 8, this.getY()+this.getHeight() - 7);
        } else if (this.angle==270) {
            this.translationPoint = new PointXY(this.getX() + 8, this.getY() + this.getHeight()-7);
        }
        return translationPoint;
    }

    public void resetInsideRGB() {
        this.r2 = this.r;
        this.g2 = this.g;
        this.b2 = this.b;
    }

    public int getB2() {
        return b2;
    }

    public void setB2(int b2) {
        this.b2 = b2;
    }

    public int getG2() {
        return g2;
    }

    public void setG2(int g2) {
        this.g2 = g2;
    }

    public int getR2() {
        return r2;
    }

    public void setR2(int r2) {
        this.r2 = r2;
    }



}
